package com.yami.shop.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Question;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.QuestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/admin/question")
@Tag(name = "问题管理")
public class QuestionController {
    @Autowired
    private QuestionService questionService;



    /**
     * 分页获取问题
     * @param question 问题
     * @param page 分页参数
     * @return 问题列表
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('admin:question:page')")
    @Operation(description = "分页获取问题")
    public ServerResponseEntity<IPage<Question>> page(Question question, PageParam<Question> page){
        return ServerResponseEntity.success(questionService.page(page,
                new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<Question>()
                        .like(ObjectUtil.isNotEmpty(question.getName()),Question::getName,question.getName())
                        .eq(ObjectUtil.isNotEmpty(question.getStatus()),Question::getStatus,question.getAnswer())
                        .orderByDesc(Question::getId)));
    }

    /**
     * 新增问题
     * @param question 问题
     * @return 添加结果
     */
    @PostMapping("save")
    @PreAuthorize("@pms.hasPermission('admin:question:save')")
    @Operation(description = "新增问题")
    public ServerResponseEntity<Void> save(@RequestBody Question question){
        questionService.save(question);
        return ServerResponseEntity.success();
    }

    /**
     * 修改问题
     * @param question 问题
     * @return 修改结果
     */
    @PutMapping("/update")
    @PreAuthorize("@pms.hasPermission('admin:question:update')")
    @Operation(description = "修改问题")
    public ServerResponseEntity<Void> update(@RequestBody Question question){
        questionService.updateById(question);
        return ServerResponseEntity.success();
    }


    /**
     * 删除问题
     * @param questionIds
     * @return
     */
    @DeleteMapping("/delete")
    @PreAuthorize("@pms.hasPermission('admin:question:delete')")
    @Operation(description = "删除问题")
    public ServerResponseEntity<Void> delete(@RequestBody Long[] questionIds){
        questionService.removeByIds(Arrays.asList(questionIds));
        return ServerResponseEntity.success();
    }




}
